package com.group1.ws;


import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Subject;
import com.group1.util.SessionFactoryUtil;


@Path("/subject")
public class SubjectWS {

	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllSubject()
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Subject> list = Subject.getAllSubject(session);
		String result;
		result = "{SUBJECT:[";
		boolean first = true;
		for (Subject s : list) {
			String subId = s.getSubjectId();
			String subName = s.getSubjectName();
			String subShortName = s.getSubjectShortname();
			int subjectCredit = s.getSubjectCredits();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"subjectID\"" + ":\"" + subId + "\",";
			result += "\"subjectName\"" + ":\"" + subName + "\",";
			result += "\"subjectShortName\"" + ":\"" + subShortName+ "\",";
			result += "\"subjectCredits\"" + ":\"" + subjectCredit+ "\"";
			result += "}";
			
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByLecturer/{lecturerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByLecturer(@PathParam("lecturerID") String lecturerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		Collection<Subject> list = Subject.getSubjectByLecturer(lecturerID, session);
		String result;
		result = "{SUBJECT:[";
		boolean first = true;
		for (Subject s : list) {
			String subId = s.getSubjectId();
			String subName = s.getSubjectName();
			String subShortName = s.getSubjectShortname();
			int subjectCredit = s.getSubjectCredits();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"subjectID\"" + ":\"" + subId + "\",";
			result += "\"subjectName\"" + ":\"" + subName + "\",";
			result += "\"subjectShortName\"" + ":\"" + subShortName+ "\",";
			result += "\"subjectCredits\"" + ":\"" + subjectCredit+ "\"";
			result += "}";
			
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getSubjectByID/{subjectID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getSubjectByID(@PathParam("subjectID") String subjectID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Subject s = Subject.getSubjectById(subjectID, session);
		String result;
		result = "{SUBJECT:";
		result += "{";
		result += "\"subjectID\"" + ":\"" + s.getSubjectId() + "\",";
		result += "\"subjectName\"" + ":\"" + s.getSubjectName() + "\",";
		result += "\"subjectShortName\"" + ":\"" + s.getSubjectShortname()+ "\",";
		result += "\"subjectCredits\"" + ":\"" + s.getSubjectCredits()+ "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/create/{subjectID}/{subjectName}/{subjectShortName}/{subjectCredits}/{lecturerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createSubject(@PathParam("subjectID") String subID, @PathParam("subjectName") String subName, 
			@PathParam("subjectShortName") String shortName, @PathParam("subjectCredits") String subjectCredits,
			@PathParam("lecturerID") String lecturerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Subject s = new Subject();
				s.setSubjectId(subID);
				s.setSubjectName(subName);
				s.setSubjectShortname(shortName);
				s.setSubjectCredits(Integer.valueOf(subjectCredits));
				s.getLecturerSubjectMapping().add(Lecturer.getLecturerByID(session, lecturerID));
				session.save(s);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{subjectID}/{subjectName}/{subjectShortName}/{subjectCredits}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateSubject(@PathParam("subjectID") String subID, @PathParam("subjectName") String subName, 
			@PathParam("subjectShortName") String shortName, @PathParam("subjectCredits") String subjectCredits)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Subject s = Subject.getSubjectById(subID, session);
				s.setSubjectName(subName);
				s.setSubjectShortname(shortName);
				s.setSubjectCredits(Integer.valueOf(subjectCredits));
				session.saveOrUpdate(s);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{subjectID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteSubject(@PathParam("subjectID") String subID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(Subject.getSubjectById(subID, session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}

}
